import axios from "axios";
import {TOKEN_HEADER} from "@/constant/app";
import useUserStore from "@/store/modules/user.js";
import router from '@/router/index.js'
import {ElMessage} from "element-plus";

export class GuiguRequest{

     instance

     defaultRequestInterceptor={
        requestInterceptor:(config)=>{
            const userStore=useUserStore()
            const userInfo=userStore.userInfo
            if(userInfo&&userInfo.loginAccessToken){
                config.headers[TOKEN_HEADER]=userInfo.loginAccessToken
            }

            return config
        },
        requestInterceptorCatch:(error)=>{
            return Promise.reject({msg:error.message})
        }
    }

     defaultResponseInterceptor={
        responseInterceptor:response=>{
            const result = response.data
            //如果后端返回错误状态码
            if(result.code===401){
                ElMessage({
                    type:"error",
                    message: result.msg,
                    duration: 3000,
                })
                const userStore=useUserStore()
                userStore.logout()
                router.push("/login").then(r => {})
                return Promise.reject(result)
            }

            if(result.code!==200){
                return Promise.reject(result)
            }

            //正常返回
            return response.data
        },

        responseInterceptorCatch:error=>{
            return Promise.reject(error.response.data?error.response.data:{msg:error.message})
        }
    }

    
    defaultSuccessFn(data){
        return data
    }

    defaultErrorFn(error){
        return Promise.reject(error)
    }
    constructor(config){
        this.instance=axios.create(config)
        this.instance.interceptors.request.use(
            this.defaultRequestInterceptor?.requestInterceptor,
            this.defaultRequestInterceptor?.requestInterceptorCatch
            )
        this.instance.interceptors.response.use(
                this.defaultResponseInterceptor.responseInterceptor,
                this.defaultResponseInterceptor.responseInterceptorCatch
            )
    }
    static  create(config){
        return new GuiguRequest(config)
    }

    upload(url,formData,params){

       return this.instance.post(url,formData,{
           'Content-type' : 'multipart/form-data'
       })
        .then(this.defaultSuccessFn)
        .catch(this.defaultErrorFn)
    }

    post(url,data,params,config){
        if(!config){
            config={

            }
        }
        config.url=url
        config.data=data
        config.params=params
        config.method="post"
        return this.request(config)
    }

    get(url,params,config){
        if(!config){
            config={

            }
        }
        config.url=url
        config.params=params
        config.method="get"
        return this.request(config)
    }

    put(url,data,params,config){
        if(!config){
            config={

            }
        }
        config.url=url
        config.data=data
        config.params=params
        config.method='put'
        return this.request(config)
    }

    delete(url,data,params,config){
        if(!config){
            config={

            }
        }
        config.url=url
        config.data=data
        config.params=params
        config.method='delete'
        return this.request(config)
    }
    request(config){
        return this.instance.request(config)
        .then(this.defaultSuccessFn)
        .catch(this.defaultErrorFn)
    }
}

const config = {
    baseURL: import.meta.env.VITE_BASE_URL,
    timeout: 5000
}

const baseRequest=GuiguRequest.create(config)

export default baseRequest


